/**
 * 
 */
package org.wmine.visualization.ui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.xml.parsers.ParserConfigurationException;

import org.jfree.data.general.Dataset;
import org.wmine.core.ProjectManager;
import org.wmine.data.Attribute;
import org.wmine.data.DataSet;
import org.wmine.data.TableResultSet;
import org.wmine.data.XMLDataReader;
import org.wmine.visualization.eventhandlers.ComboAlgoEventHandler;
import org.wmine.visualization.eventhandlers.DisplayGraphEventHandler;
import org.xml.sax.SAXException;

/**
 * @author Administrator
 *
 */
public class AttributeSelectionWindow {
	private String inputFileName;
	 
	private String attribute1;
	private String attribute2;
	JFrame attribSelectionWindow;
	DisplayGraphEventHandler dispBtnAlgoHandler;
	public AttributeSelectionWindow(String selectedVisualizeMethod, String fileName, DisplayGraphEventHandler displayBtnEventHandler) {
		// TODO Auto-generated constructor stub
			
		dispBtnAlgoHandler = displayBtnEventHandler;
		inputFileName = fileName;
		
		
		//System.out.println("    tt :"+ selectedVisualizeMethod+" ttt :"+inputFileName);
		attribSelectionWindow = new JFrame();
	//	container=this.getContentPane();
		attribSelectionWindow.setBounds(300,200,300,250);
		if (selectedVisualizeMethod.equals("Scatter Plot")) {
			attribSelectionWindow.setTitle("Scatter Plot");
			addInputAttributeWindow();
		}
		else
			if (selectedVisualizeMethod.equals("Bar Chart")) {
				attribSelectionWindow.setTitle("Bar Chart");
				addInputAttributeWindow();
			}
		
	
	}

	private void addInputAttributeWindow() {
		// TODO Auto-generated method stub
		 attribute1 = new String();
		 attribute2 = new String();
		JPanel basePanel =new JPanel();
		basePanel.setLayout(null);
		basePanel.setBounds(attribSelectionWindow.getBounds());
		
		JLabel attr1Label = new JLabel("Select Atribute for X-Axis :");
		attr1Label.setBounds(20,10,150,20);
		
		JComboBox attr1Combo = new JComboBox();
		attr1Combo.setBounds(180,10,100,20);
		loadAttributes(attr1Combo);	
		
		attr1Combo.addItemListener(new ItemListener(){
			String temp;
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				
			 		attribute1 = e.getItem().toString();
	 		// System.out.println("in AttributeSelectionWindow x attrib "+attribute1);
			 		dispBtnAlgoHandler.setPreprocessVisualizeAttribute1(attribute1);
			
			}
			
		});
		
		JLabel attr2Label = new JLabel("Select Atribute for Y-Axis :");
		attr2Label.setBounds(20,70,150,20);
		
		JComboBox attr2Combo = new JComboBox();
		attr2Combo.setBounds(180,70,100,20);
		loadAttributes(attr2Combo);	
		
		attr2Combo.addItemListener(new ItemListener(){
			String temp;
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
					attribute2 = e.getItem().toString();
				//	System.out.println("in AttributeSelectionWindow y attrib "+attribute2);
					dispBtnAlgoHandler.setPreprocessVisualizeAttribute2(attribute2);						
			}
			
		});
		
		JButton btnOk = new JButton("Ok");
		btnOk.setBounds(100,150,80,20);
		
		  //	if( !(attribute1.equals(null) && attribute2.equals(null))){	
		  		//System.out.println("in atribute null comparison" +attribute1+" "+attribute2);
		  		
		  	
		  			
		  //	}
		  	
		  	dispBtnAlgoHandler.setAttribSelectionWindow(attribSelectionWindow);
		  	dispBtnAlgoHandler.setInputPreprocessFileName(inputFileName);
			btnOk.addActionListener(dispBtnAlgoHandler);
			
		/*
		btnOk.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			
				attribSelectionWindow.dispose();
				
				}
			}
			
		});*/
		
		basePanel.add(attr1Label);
		basePanel.add(attr1Combo);
		basePanel.add(attr2Label);
		basePanel.add(attr2Combo);
		basePanel.add(btnOk);
		
		attribSelectionWindow.add(basePanel);
		this.setAttribSelectionWindow(attribSelectionWindow);
	}

	private void loadAttributes(JComboBox attr1Combo) {
		// TODO Auto-generated method stub
		DataSet dataSet = new DataSet();
		try {
			if (inputFileName.endsWith(".xml")) {
				dataSet.setDataReader(new XMLDataReader(new StringBufferInputStream(ProjectManager.getUniqueInstance().readFile(inputFileName))));	
			}
			
			TableResultSet resultSet = dataSet.getTableResultSet();
			Iterator<Attribute> iteratorAttribute =	dataSet.getAttributeIterator();
			attr1Combo.addItem("-- Select --");
			while (iteratorAttribute.hasNext()) {
				Attribute element = (Attribute) iteratorAttribute.next();
				attr1Combo.addItem(element.getAttributeName());
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		//attr1Combo
	}

	public JFrame getAttribSelectionWindow() {
		return attribSelectionWindow;
	}

	public void setAttribSelectionWindow(JFrame attribSelectionWindow) {
		this.attribSelectionWindow = attribSelectionWindow;
	}

}
